#region License
// Copyright (c) 2007 James Newton-King
//
// Permission is hereby granted, free of charge, to any person
// obtaining a copy of this software and associated documentation
// files (the "Software"), to deal in the Software without
// restriction, including without limitation the rights to use,
// copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the
// Software is furnished to do so, subject to the following
// conditions:
//
// The above copyright notice and this permission notice shall be
// included in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
// OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
// HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
// WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
// OTHER DEALINGS IN THE SOFTWARE.
#endregion

using System;
using System.Collections.Generic;
using System.Text;
using NUnit.Framework;
using Utilities.Miscellaneous;
using System.Xml;
using Utilities.Testing;
using Utilities.Xml;

namespace Utilities.Test.Xml
{
  public class XPathUtilsFixture : TestFixtureBase
  {
    private XmlDocument _doc;

    public override void FixtureSetup()
    {
      _doc = new XmlDocument();
      _doc.LoadXml(@"<bib>
    <book year=""1992"" joe=""1,2,3"">
        <title>Advanced Programming in the Unix environment</title>
        <author><last>Stevens</last><first>W.</first></author>
        <author><last>Golde</last><first>Peter</first></author>
        <publisher>Addison-Wesley</publisher>
        <price>65.95</price>
    </book>
    <book year=""2000"">
        <title>Data on the Web</title>
        <author><last>Abiteboul</last><first>Serge</first></author>
        <author><last>Buneman</last><first>Peter</first></author>
        <author><last>Suciu</last><first>Dan</first></author>
        <publisher>Morgan Kaufmann Publishers</publisher>
        <price>39.95</price>
    </book>
    <book year=""1999"">
        <title>The Economics of Technology and Content for Digital TV</title>
        <author><last>Hejlsberg</last><first>Anders</first></author>
        <editor>
               <last>Gerbarg</last><first>Darcy</first>
                <affiliation type=""informal"">CITI</affiliation>
        </editor>
            <publisher>Kluwer Academic Publishers</publisher>
        <price>129.95</price>
    </book>
</bib>");
    }

    [Test]
    public void TrySelectNodeText()
    {
      string selectedText;
      bool result;

      result = XPathUtils.TrySelectNodeText(_doc, "/bib/book[@year='1992']/title/text()", out selectedText);
      Assert.IsTrue(result);
      Assert.AreEqual("Advanced Programming in the Unix environment", selectedText);

      result = XPathUtils.TrySelectNodeText(_doc, "/bib/book[@year='1992']/@joe", out selectedText);
      Assert.IsFalse(result);
      Assert.IsNull(selectedText);

      result = XPathUtils.TrySelectNodeText(null, "/bib/book[@year='1992']/@joe", out selectedText);
      Assert.IsFalse(result);
      Assert.IsNull(selectedText);

      result = XPathUtils.TrySelectNodeText(null, null, out selectedText);
      Assert.IsFalse(result);
      Assert.IsNull(selectedText);
    }

    #region SelectNodeText
    [Test]
    public void SelectNodeText()
    {
      for (int i = 0; i < 10000; i++)
      {
        string selectedText;

        selectedText = XPathUtils.SelectNodeText(_doc, "/bib/book[@year='1992']/author[1]/last/text()");
        Assert.AreEqual("Stevens", selectedText);
      }
    }

    [Test]
    [ExpectedException(typeof(XmlException), ExpectedMessage = "XPath expression '/bib/book[@year='1992']/author[1]' returned a non-text node.")]
    public void SelectNodeTextSelectNonText()
    {
      XPathUtils.SelectNodeText(_doc, "/bib/book[@year='1992']/author[1]");
    }

    [Test]
    [ExpectedException(typeof(XmlException), ExpectedMessage = "XPath expression '/purplemonkey' did not return a node.")]
    public void SelectNodeTextSelectNothing()
    {
      XPathUtils.SelectNodeText(_doc, "/purplemonkey");
    }

    [Test]
    [ExpectedException(typeof(ArgumentNullException), ExpectedMessage = @"Value cannot be null.
Parameter name: navigable")]
    public void SelectNodeTextNullNode()
    {
      XPathUtils.SelectNodeText(null, "/purplemonkey");
    }

    [Test]
    [ExpectedException(typeof(ArgumentNullException), ExpectedMessage = @"Value cannot be null.
Parameter name: xpath")]
    public void SelectNodeTextNullXPath()
    {
      XPathUtils.SelectNodeText(_doc, null);
    }
    #endregion

    #region SelectNodeXml
    [Test]
    public void SelectNodeXml()
    {
      string selectedText;

      selectedText = XPathUtils.SelectNodeXml(_doc, "/bib/book[@year='1992']/author[1]/last/text()");
      Assert.AreEqual(string.Empty, selectedText);

      selectedText = XPathUtils.SelectNodeXml(_doc, "/bib/book[@year='1992']/author[1]/last");
      Assert.AreEqual("Stevens", selectedText);

      selectedText = XPathUtils.SelectNodeXml(_doc, "/bib/book[@year='1992']/author[1]");
      Assert.AreEqual(@"<last>Stevens</last>
<first>W.</first>", selectedText);
    }

    [Test]
    [ExpectedException(typeof(XmlException), ExpectedMessage = "XPath expression '/purplemonkey' did not return a node.")]
    public void SelectNodeXmlSelectNothing()
    {
      XPathUtils.SelectNodeXml(_doc, "/purplemonkey");
    }

    [Test]
    [ExpectedException(typeof(ArgumentNullException), ExpectedMessage = @"Value cannot be null.
Parameter name: navigable")]
    public void SelectNodeXmlNullNode()
    {
      XPathUtils.SelectNodeXml(null, "/purplemonkey");
    }

    [Test]
    [ExpectedException(typeof(ArgumentNullException), ExpectedMessage = @"Value cannot be null.
Parameter name: xpath")]
    public void SelectNodeXmlNullXPath()
    {
      XPathUtils.SelectNodeXml(_doc, null);
    }
    #endregion

    [Test]
    public void TrySelectNodeXml()
    {
      string selectedText;
      bool result;

      result = XPathUtils.TrySelectNodeXml(_doc, "/bib/book[@year='1992']/title/text()", out selectedText);
      Assert.IsTrue(result);
      Assert.AreEqual(string.Empty, selectedText);

      result = XPathUtils.TrySelectNodeXml(_doc, "/bib/book[@year='1992']/@joe", out selectedText);
      Assert.IsTrue(result);
      Assert.AreEqual("1,2,3", selectedText);

      result = XPathUtils.TrySelectNodeXml(_doc, "/bib/book[@year='2000']/author[2]", out selectedText);
      Assert.IsTrue(result);
      Assert.AreEqual(@"<last>Buneman</last>
<first>Peter</first>", selectedText);
    }
  }
}